package br.indt.framework.scheduler;


/**
 *
 * @author efirmino
 */
class JobQueue {
    private final PriorityQueue queue;

    public JobQueue() {
        queue = new PriorityQueue();
    }

    public synchronized void add(Job job) {
        queue.add(job, Timer.currentTimestamp() + job.getDelay());
    }

    public synchronized void remove(Job job) {
        queue.remove(job);
    }

    public synchronized void remove(Task task) {
        for(int i = 0; i < queue.size(); i++) {
            Job other = (Job) queue.get(i);
            if (other.getTask() == task) {
                queue.remove(i);
                return;
            }
        }
    }

    public synchronized void removeAll() {
        queue.removeAll();
    }

    public synchronized int getJobTimeStamp(int index) {
        return queue.getPriority(index);
    }

    public synchronized boolean isEmpty() {
        return queue.isEmpty();
    }

    public synchronized int size() {
        return queue.size();
    }

    public synchronized Job peek() {
        if (queue.isEmpty()) {
            return null;
        }

        return (Job) queue.first();
    }

    public synchronized Job poll() {
        if (queue.isEmpty()) {
            return null;
        }

        Job job = (Job) queue.first();
        queue.remove(0);
        return job;
    }
}
